package com.aaa.power.controller;

import com.aaa.common.constant.ExceptionConstant;
import com.aaa.common.constant.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.power.entity.Menu;
import com.aaa.power.entity.User;
import com.aaa.power.service.MenuService;
import org.apache.shiro.session.Session;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 菜单权限表(Menu)表控制层
 *
 * @author makejava
 * @since 2020-11-09 16:31:27
 */
@RestController
@RequestMapping("menu")
public class MenuController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        //当ID空为时抛出自定义异常
        if (null == id) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.menuService.queryById(id));
    }

    /**
     * 根据用户id查询功能菜单
     * @return
     */
    @GetMapping("queryMenuByUserId")
    public Result queryMenuByUserId(Integer userId){
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.menuService.queryMenuByUserId(userId));
    }
    /**
     * 查找功能菜单树形结构
     * @return
     */
    @GetMapping("queryMenuTree")
    public Result queryMenuTree() {
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.menuService.queryAllToTreeData());
    }
    @PostMapping("insert")
    public Result insert(@RequestBody Menu menu) {
        //当menu为空时抛出自定义异常
        if (menu==null) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.menuService.insert(menu));
    }

    /**
     * 查询非空可用url，用于前台做动态路由
     * @return
     */
    @GetMapping("queryUrlNotNull")
    public Result queryUrlNotNull(){
        return menuService.queryUrlNotNull();
    }

    @PostMapping("update")
    public Result update(@RequestBody Menu menu) {
        System.out.println(menu);
        //当menu为空时抛出自定义异常
        if (menu==null) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.menuService.update(menu));
    }
    @DeleteMapping("deleteById")
    public Result deleteById(Integer menuId) {
        //当ID为空时抛出自定义异常
        if (menuId==null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        //返回成功
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(), this.menuService.deleteById(menuId));
    }
}